package com.web.daoimpl;


import com.web.dao.TestBiaoDao;
import com.web.entity.TestBiao;
import com.web.util.DatabaseUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

/**
 * 考试表的实现
 * @author 12270
 */
public class TestBiaoDaoImpl extends BaseDaoImpl implements TestBiaoDao{

    /**
     *
     * @param testid
     * @return
     */
    @Override
    public TestBiao searchStatus(int testid) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        TestBiao test = null;
        
        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select istestfinish,courseid,classid,tid from Test where testid=?");
            ps.setInt(1, testid);
            rs = ps.executeQuery();
            if (rs.next()) {
                test = new TestBiao(rs.getInt(1),rs.getInt(2),rs.getInt(3),rs.getInt(4));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
    return test;
    }
/**
     *管理员用于修改考试表的状态信息
     * @param testid
     * @return
     */
    @Override
    public int modify_status(int testid) {
        int[] i=new int[]{testid,testid};
        return update("if((select isTestfinish from test where TestID=?)=0)begin update test set isTestfinish=1 where TestID=? end", i);
    }

    @Override
    public int isStatusequal0(int testid) {
        int status=1;//初识值为1，表示已申请考试
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        TestBiao test = null;
        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select isTestfinish from test where TestID=?");
            ps.setInt(1, testid);
            rs = ps.executeQuery();
            if (rs.next()) {
                status=rs.getInt(1);
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
        return status;
    }

    }

